package com.tingyuge.base.jdk.io.nio.filechannel;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

/**
 * @author xiaodao
 * @version 1.0
 * @description transferFrom
 */
public class FileChannelDemo3 {
    public static void main(String[] args) throws Exception {
    //创建2个fileChennel

        String path = FileChannelDemo2.class.getResource("/").getPath();
        String transferFrom1 = path.replace("target/classes/", "src/main/resources/filechennel/transferfrom1.txt");
        RandomAccessFile accessFile1 = new RandomAccessFile(transferFrom1,"rw");


        String transferFrom2 = path.replace("target/classes/", "src/main/resources/filechennel/transferfrom2.txt");
        RandomAccessFile accessFile2 = new RandomAccessFile(transferFrom2,"rw");


        FileChannel channel1 = accessFile1.getChannel();
        System.out.println(channel1.size());
        FileChannel channel2 = accessFile2.getChannel();
        //channel1传输到  channel2

        long position = 0;
        long size = channel1.size();

        channel2.transferFrom(channel1, position, size);


        channel1.close();
        channel2.close();
    }
}
